﻿using System;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel;
using System.Workflow.ComponentModel;

namespace TPG.SharePoint.Workflow.Activities.Design
{
    public class OnWorkflowActivatedDesigner
        : ActivityDesigner
    {
        protected override void PreFilterProperties(System.Collections.IDictionary properties)
        {
            OnWorkflowActivated activity= (OnWorkflowActivated)Activity;
            base.PreFilterProperties(properties);
            if (activity.AssociationDataType != null)
            {
                properties.Add(OnWorkflowActivated.AssociationDataProperty, 
                    new VirtualPropertyDescriptor(
                        OnWorkflowActivated.AssociationDataProperty, 
                        activity.AssociationDataType));
            }
            if (activity.InstantiationDataType != null)
            {
                properties.Add(OnWorkflowActivated.InstantiationDataProperty, 
                    new VirtualPropertyDescriptor(
                        OnWorkflowActivated.InstantiationDataProperty, 
                        activity.InstantiationDataType));
            }
        }

        class VirtualPropertyDescriptor
            : PropertyDescriptor
        {
            Type _dataType;

            public override Type ComponentType
            {
                get { return typeof(OnWorkflowActivated); }
            }

            public override bool IsReadOnly
            {
                get { return false; }
            }

            public override Type PropertyType
            {
                get { return _dataType; }
            }

            public override TypeConverter Converter
            {
                get { return new ActivityBindTypeConverter(); }
            }

            public VirtualPropertyDescriptor(string name, Type dataType)
                : base( name, new Attribute[]{ DesignOnlyAttribute.Yes})
            {
                _dataType = dataType;
            }

            public override bool CanResetValue(object component)
            {
                return false;
            }

            public override object GetValue(object component)
            {
                OnWorkflowActivated activity = (OnWorkflowActivated)component;
                return activity.GetVirtualProperty(Name);
            }

            public override void SetValue(object component, object value)
            {
                OnWorkflowActivated activity = (OnWorkflowActivated)component;
                activity.SetVirtualProperty(Name, value);
                OnValueChanged(component, EventArgs.Empty);
            }

            public override void ResetValue(object component)
            {
                throw new NotImplementedException();
            }

            public override bool ShouldSerializeValue(object component)
            {
                return true;
            }
        }

    }
}
